package com.dragonfly.shutter.controllers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.dragonfly.shutter.DAOs.AlbumDAO;
import com.dragonfly.shutter.DAOs.TagDAO;
import com.dragonfly.shutter.beans.Album;
import com.dragonfly.shutter.beans.Tag;
import com.dragonfly.shutter.utils.ListOperation;
import com.dragonfly.shutter.validator.AlbumValidator;

/**
 * 新建相册的控制器
 * 修改创建相册的代码:5.20,黄挺
 * @author 王长乐
 *
 */
@Controller
@RequestMapping("/createAlbum.html")
@SessionAttributes("userID")
public class CreateAlbumController {
	@Autowired
	private AlbumDAO albumDAO; //相册DAO
	@Autowired
	private TagDAO tagDAO; //标签DAO

	@RequestMapping(method = RequestMethod.GET)
	public String insertAlbumProcess(ModelMap model,
			@ModelAttribute("userID") Integer userID,
			BindingResult result,SessionStatus status)
	{
		if(userID==null)
		{
			return "login";
		}
		else{
			
			Album album=new Album();
			
			model.addAttribute(album);
			
			return "createAlbum";
			
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView insertAlbumProcess(ModelMap model,
			@ModelAttribute("userID") Integer userID,
			@ModelAttribute("album") Album album, BindingResult result,
            @RequestParam(value="albumTags", required=true) String albumTags,
			SessionStatus status)
	{
		if(userID==null)
		{
			return new ModelAndView("redirect:/login.html");
		}
		else
		{
			
		if( album== null  ){
			
			return new ModelAndView("redirect:/user.html?userID="+userID);
			
		}
		else{
			int errorCount = result.getErrorCount();
			
			new AlbumValidator().validate(album, result);
			
			if(errorCount != result.getErrorCount())
			{
			        return new ModelAndView("createAlbum");
			}
			else
			{
				//检查相册是否重名
				List<Album> albumList=albumDAO.selectAlbumByUserID(userID);
				Iterator<Album> iter=albumList.iterator();
				while(iter.hasNext())
				{
					Album existAlbum=(Album)iter.next();
					if(existAlbum.getAlbumName().equals(album.getAlbumName()))
					{
						return new ModelAndView("createAlbum");
						
					}
			
				}
				
				//将新标签加入到Album Bean里
				List<String> tags = ListOperation.stringToList(albumTags, "\\s{1,}");
				List<String> tagNew = tags;
				album.setAlbumTags(new ArrayList<Tag>());
				
				for ( String str : tags ){
					Tag tag = new Tag();
					tag.setTagName(str);
					album.getAlbumTags().add(tag);
				}
				
				//生成新的Album
				album.setFlagSortType("name");		//默认按名字排序
				album.getOwner().setUserID(userID);
				albumDAO.insertAlbum(album);
				Integer albumID = albumDAO.selectAlbumByNameAndUserId(userID, album.getAlbumName()).getAlbumID();
				album.setAlbumID(albumID);
				
				//标签DAO操作
				for(String str : tagNew){
					Tag tag = tagDAO.selectTagByName(str);
					
					if( tag == null ){
						tag = new Tag();
						
						if(str != null && !str.trim().equals("")){
							tag.setTagName(str);
							tagDAO.insertTag(tag);
							tag = tagDAO.selectTagByName(str);
							tagDAO.insertAlbumTag(album.getAlbumID(), tag.getTagID());
						}
					}
				}
								
				return new ModelAndView("redirect:/user.html");
				
			}
			
				
		}
			
		}
		}
	}
	
	
